<!DOCTYPE HTML>
<html lang>
<head><meta name="generator" content="Hexo 3.9.0">
    <!--Setting-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta http-equiv="Cache-Control" content="no-transform">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="apple-mobile-web-app-capable" content="腾宇的个人博客">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,adress=no">
    <meta name="browsermode" content="application">
    <meta name="screen-orientation" content="portrait">
    <meta name="theme-version" content="1.2.3">
    <meta name="root" content="/blog/">
    <link rel="dns-prefetch" href="https://gitee.com/bingtengaoyu/blog">
    <!--SEO-->

    <meta name="keywords" content>


    <meta name="description" content="console的用法@(作者)[腾宇]
1. 基础用法——console.log()1234567// 打印一段文本console.log('Is this working?');// 打印对象...">



<meta name="robots" content="all">
<meta name="google" content="all">
<meta name="googlebot" content="all">
<meta name="verify" content="all">

    <!--Title-->


<title>console的用法 | 腾宇的个人博客</title>


    <link rel="alternate" href="/atom.xml" title="腾宇的个人博客" type="application/atom+xml">


    <link rel="icon" href="/favicon.ico">

    



<link rel="stylesheet" href="/blog/css/bootstrap.min.css?rev=3.3.7">
<link rel="stylesheet" href="/blog/css/font-awesome.min.css?rev=4.5.0">
<link rel="stylesheet" href="/blog/css/style.css?rev=@@hash">




    
	<div class="hide">
		<script type="text/javascript">
			var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan class='cnzz_stat_icon_1263868967 hide' %3E%3Cscript%20src%3D%22https%3A%2F%2Fs95.cnzz.com%2Fz_stat.php%3Fweb_id%3D1272564536%22%3E%3C%2Fscript%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s19.cnzz.com/z_stat.php%3Fid%3D1263868967%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
		</script>
	</div>






    

</head>

</html>
<!--[if lte IE 8]>
<style>
    html{ font-size: 1em }
</style>
<![endif]-->
<!--[if lte IE 9]>
<div style="ie">你使用的浏览器版本过低，为了你更好的阅读体验，请更新浏览器的版本或者使用其他现代浏览器，比如Chrome、Firefox、Safari等。</div>
<![endif]-->

<body>
    <header class="main-header"  style="background-image:url(http://snippet.shenliyang.com/img/banner.jpg)"  >
    <div class="main-header-box">
        <a class="header-avatar" href="/" title='耿兵兵'>
            <img src="/blog/img/avatar.jpg" alt="logo头像" class="img-responsive center-block">
        </a>
        <div class="branding">
        	<!--<h2 class="text-hide">Snippet主题,从未如此简单有趣</h2>-->
            
                 <img src="/blog/img/branding.png" alt="Snippet 博客主题" class="img-responsive center-block">
            
    	</div>
    </div>
</header>
    <nav class="main-navigation">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="navbar-header"><span class="nav-toggle-button collapsed pull-right" data-toggle="collapse" data-target="#main-menu" id="mnav">
                    <span class="sr-only"></span>
                        <i class="fa fa-bars"></i>
                    </span>
                    <a class="navbar-brand" href="https://gitee.com/bingtengaoyu/blog">腾宇的个人博客</a>
                </div>
                <div class="collapse navbar-collapse" id="main-menu">
                    <ul class="menu">
                        
                            <li role="presentation" class="text-center">
                                <a href="/blog"><i class="fa "></i>Home</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="https://bingtengaoyu.gitee.io/vuetools/#/"><i class="fa "></i>vue插件</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="https://bingtengaoyu.gitee.io/reactantd/#/"><i class="fa "></i>reactAnt模板</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="https://bingtengaoyu.gitee.io/home/"><i class="fa "></i>简介</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="http://gank.io/"><i class="fa "></i>工具</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="/blog/archives/"><i class="fa "></i>时间轴</a>
                            </li>
                        
                    </ul>
                </div>
            </div>
        </div>
    </div>
</nav>
    <section class="content-wrap">
        <div class="container">
            <div class="row">
                <main class="col-md-8 main-content m-post">
                    <p id="process"></p>
<article class="post">
    <div class="post-head">
        <h1 id="console的用法">
            
	            console的用法
            
        </h1>
        <div class="post-meta">
    
        <span class="categories-meta fa-wrap">
            <i class="fa fa-folder-open-o"></i>
            
        </span>
    

    
        <span class="fa-wrap">
            <i class="fa fa-tags"></i>
            <span class="tags-meta">
                
            </span>
        </span>
    

    
        
        <span class="fa-wrap">
            <i class="fa fa-clock-o"></i>
            <span class="date-meta">2019/07/14</span>
        </span>
        
    
</div>
            
            
    </div>
    
    <div class="post-body post-content">
        <h1 id="console的用法"><a href="#console的用法" class="headerlink" title="console的用法"></a>console的用法</h1><p>@(作者)[腾宇]</p>
<h3 id="1-基础用法——console-log"><a href="#1-基础用法——console-log" class="headerlink" title="1. 基础用法——console.log()"></a>1. 基础用法——console.log()</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 打印一段文本</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'Is this working?'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 打印对象</span></span><br><span class="line"><span class="keyword">const</span> foo = &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">verified</span>: <span class="literal">true</span>, <span class="attr">color</span>: <span class="string">'green'</span> &#125;;</span><br><span class="line"><span class="keyword">const</span> bar = &#123; <span class="attr">id</span>: <span class="number">2</span>, <span class="attr">verified</span>: <span class="literal">false</span>, <span class="attr">color</span>: <span class="string">'red'</span> &#125;;</span><br><span class="line"><span class="built_in">console</span>.log (&#123;foo, bar&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="2-console-table"><a href="#2-console-table" class="headerlink" title="2. console.table()"></a>2. console.table()</h3><p><strong>为了增加输出的可读性，我们可以使用 console.table () 让其通过表格的形式进行展示，比如我们在控制台输出 console.table ({ foo, bar})</strong></p>
<h3 id="3-console-group"><a href="#3-console-group" class="headerlink" title="3. console.group()"></a>3. console.group()</h3><p><strong>有时候，为了方便分析，你需要将一些对象变量等信息进行分组打印输出，示例如下：</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.group(<span class="string">'User Details'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'name: John Doe'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'job: Software Developer'</span>);</span><br><span class="line"><span class="comment">// Nested Group</span></span><br><span class="line"><span class="built_in">console</span>.group(<span class="string">'Address'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'Street: 123 Townsend Street'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'City: San Francisco'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'State: CA'</span>);</span><br><span class="line"><span class="built_in">console</span>.groupEnd();</span><br><span class="line"><span class="built_in">console</span>.groupEnd();</span><br></pre></td></tr></table></figure>

<h3 id="4-console-warn-amp-console-error"><a href="#4-console-warn-amp-console-error" class="headerlink" title="4. console.warn() &amp; console.error()"></a>4. console.warn() &amp; console.error()</h3><p><strong>一些特别的信息我们需要特别强调醒目的输出出来，比如警告和错误信息，你可以使用 console.warn () &amp; console.error (),你还可以更进一步，自定义打印输出的样式，你可以使用 %c 语法。你可以使用这个方法更好的区分定义来源你的哪个 API 调用，用户事件，示例如下：</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">'%c Auth '</span>,</span><br><span class="line">    <span class="string">'color: white; background-color: #2274A5'</span>,</span><br><span class="line">    <span class="string">'登录验证通过'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'%c GraphQL '</span>,</span><br><span class="line">    <span class="string">'color: white; background-color: #95B46A'</span>,</span><br><span class="line">    <span class="string">'获取用户信息'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'%c Error '</span>,</span><br><span class="line">    <span class="string">'color: white; background-color: #D33F49'</span>,</span><br><span class="line">    <span class="string">'获取用户信息失败'</span>);</span><br></pre></td></tr></table></figure>

<h3 id="5-console-trace"><a href="#5-console-trace" class="headerlink" title="5. console.trace()"></a>5. console.trace()</h3><p><strong>console.trace () 方法用于显示当前执行的代码在堆栈中的调用路径。如下段代码所示，在控制台里清晰了展示了 add () 的调用轨迹</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.trace();</span><br><span class="line">    <span class="keyword">return</span> a+b;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add3</span>(<span class="params">a,b</span>)</span>&#123;<span class="keyword">return</span> add2(a,b);&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add2</span>(<span class="params">a,b</span>)</span>&#123;<span class="keyword">return</span> add1(a,b);&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add1</span>(<span class="params">a,b</span>)</span>&#123;<span class="keyword">return</span> add(a,b);&#125;</span><br><span class="line"><span class="keyword">var</span> x = add3(<span class="number">1</span>,<span class="number">1</span>);</span><br></pre></td></tr></table></figure>

<h3 id="6-console-time"><a href="#6-console-time" class="headerlink" title="6. console.time()"></a>6. console.time()</h3><p><strong>我们在开发过程中，经常需要知道一个方法运行了多长时间，方便我们做出修改完善的判断，我们可以使用 console.time ()，示例如下：</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">console</span>.time(<span class="string">"While loop"</span>);</span><br><span class="line"><span class="keyword">while</span> (i &lt; <span class="number">1000000</span>) &#123;</span><br><span class="line">    i++;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.timeEnd(<span class="string">"While loop"</span>);</span><br><span class="line"><span class="built_in">console</span>.time(<span class="string">"For loop"</span>);</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">1000000</span>; i++) &#123;</span><br><span class="line">    <span class="comment">// For Loop</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.timeEnd(<span class="string">"For loop"</span>);</span><br></pre></td></tr></table></figure>

<h3 id="7-console-dir"><a href="#7-console-dir" class="headerlink" title="7. console.dir()"></a>7. console.dir()</h3><p><strong>console.dir () 可以直观展示一个对象的所有属性和方法，示例代码如下：</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">cat</span>(<span class="params">name, age, score</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.name = name;</span><br><span class="line">    <span class="keyword">this</span>.age = age;</span><br><span class="line">    <span class="keyword">this</span>.score = score;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> c = <span class="keyword">new</span> cat(<span class="string">"miao"</span>, <span class="number">2</span>, [<span class="number">6</span>,<span class="number">8</span>,<span class="number">7</span>]);</span><br><span class="line"><span class="built_in">console</span>.dir(c);</span><br></pre></td></tr></table></figure>


    </div>
    
    <div class="post-footer">
        <div>
            
                转载声明：商业转载请联系作者获得授权,非商业转载请注明出处 © <a href="" target="_blank">Snippet</a>
            
        </div>
        <div>
            
        </div>
    </div>
</article>

<div class="article-nav prev-next-wrap clearfix">
    
        <a href="/blog/2019/07/14/async与await/" class="pre-post btn btn-default" title='async/await 理解'>
            <i class="fa fa-angle-left fa-fw"></i><span class="hidden-lg">上一篇</span>
            <span class="hidden-xs">async/await 理解</span>
        </a>
    
    
        <a href="/blog/2019/07/14/数组的 api 归纳总结/" class="next-post btn btn-default" title='数组的 api 归纳总结'>
            <span class="hidden-lg">下一篇</span>
            <span class="hidden-xs">数组的 api 归纳总结</span><i class="fa fa-angle-right fa-fw"></i>
        </a>
    
</div>


    <div id="comments">
        
	
    <div id="vcomments" class="valine"></div>
    <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="/blog/assets/valine.min.js"></script>

    <script>
        new Valine({
            av: AV,
            el: '#vcomments',
            appId: 'xOKV9J4UeQAtVkvnJC7Kq2Jn-gzGzoHsz',
            appKey: 'erIpQac4azoCmgfBB7Dl9maa',
            placeholder: '说点什么吧',
            notify: false,
            verify: true,
            avatar: 'mm',
            meta: 'nick,mail'.split(','),
            pageSize: '10',
            path: window.location.pathname,
            lang: ''.toLowerCase()
        })
    </script>


    </div>





                </main>
                
                    <aside id="article-toc" role="navigation" class="col-md-4">
    <div class="widget">
        <h3 class="title">Table of Contents</h3>
        
            <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#console的用法"><span class="toc-text">console的用法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-基础用法——console-log"><span class="toc-text">1. 基础用法——console.log()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-console-table"><span class="toc-text">2. console.table()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-console-group"><span class="toc-text">3. console.group()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-console-warn-amp-console-error"><span class="toc-text">4. console.warn() &amp; console.error()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-console-trace"><span class="toc-text">5. console.trace()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-console-time"><span class="toc-text">6. console.time()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-console-dir"><span class="toc-text">7. console.dir()</span></a></li></ol></li></ol></li></ol>
        
    </div>
</aside>

                
            </div>
        </div>
    </section>
    <footer class="main-footer">
    <div class="container">
        <div class="row">
        </div>
    </div>
</footer>

<a id="back-to-top" class="icon-btn hide">
	<i class="fa fa-chevron-up"></i>
</a>




    <div class="copyright">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="busuanzi">
    
</div>

            </div>
            <div class="col-sm-12">
                <span>Copyright &copy; 2017
                </span> |
                <span>
                    Powered by <a href="//hexo.io" class="copyright-links" target="_blank" rel="nofollow">Hexo</a>
                </span> |
                <span>
                    Theme by <a href="//github.com/shenliyang/hexo-theme-snippet.git" class="copyright-links" target="_blank" rel="nofollow">Snippet</a>
                </span>
            </div>
        </div>
    </div>
</div>







<script src="/blog/js/app.js?rev=@@hash"></script>

</body>
</html>